package com.trans.handler;

import com.trans.anno.Translator;
import com.trans.auxiliary.HandleFullRecord;
import com.trans.auxiliary.HandleRecord;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 *@Description TODO
 *@Author xwduo
 *@Date 2022年03月01日 08:51
 **/
@Slf4j
public class Date implements TranslateHandle<String,Object> {

    private static final String DEFAULT_FORMAT = "yyyy-MM-dd";

    public static final String PREFIX = "DATE:";

    @Override
    @SuppressWarnings("unchecked")
   public Object doHandle(HandleRecord record, Map<String, Object> cacheMap) {
        Object curItemVal = record.getVal();
        if (null != curItemVal && curItemVal.getClass() == java.util.Date.class) {
            Translator translator = record.translator;
            List<String> dicText = fetchParams(translator.extraParams(),PREFIX);
            String format = DEFAULT_FORMAT;
            if (null != dicText && dicText.size() > 0) {
                format = dicText.get(0);
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.format(curItemVal);
        }
        return null;
    }

   @Override
   public Map<String, Object> cache(List<HandleFullRecord> recordList) {
       return  null;
   }

    @Override
    public int order() {
        return 0;
    }

}
